package com.dongjiantong.web.protal.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.DicIllness;
import com.dongjiantong.sys.service.DicIllnessService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * @author Lee
 * @date 2018/1/9
 */
@Slf4j
@OpenBean
public class DicIllnessController {

    @Reference(version = "1.0.0")
    public DicIllnessService dicIllnessService;

    /**
     * 养殖档案根据动物种类获取病种 (检测项目)
     * 字符串的病种名称
     */
    @OpenMethod(key = "web.farmrecords.obtainDicIllnessName",
            paramNames = {},
            httpMethods = RequestMethod.GET)
    public Response<List> obtainDicIllnessName(){
        try {
            if (dicIllnessService == null){
                throw new OPServerException("dicIllnessService 为null,请检查");
            }

            List<DicIllness> dicIllnessList = dicIllnessService.searchIllness(null);
            List list = Lists.newArrayList();
            if (dicIllnessList.size() > 0){
                for (DicIllness dicIllness : dicIllnessList){
                    Map map = Maps.newHashMap();
                    map.put("value",dicIllness.getIllname());
                    list.add(map);
                }
                //RedisUtil.setList(key,list);
            }
            return Response.ok(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    /**
     * 养殖档案根据动物种类获取病种 (检测项目)
     * 数值的病种编号,字符串的病种名称
     */
    @OpenMethod(key = "web.farmrecords.obtainDicIllness",
            paramNames = {},
            httpMethods = RequestMethod.GET)
    public Response<List> obtainDicIllness(){
        try {
            if (dicIllnessService == null){
                throw new OPServerException("dicIllnessService 为null,请检查");
            }

            List<DicIllness> dicIllnessList = dicIllnessService.searchIllness(null);
            List list = Lists.newArrayList();
            if (dicIllnessList.size() > 0){
                for (DicIllness dicIllness : dicIllnessList){
                    Map map = Maps.newHashMap();
                    map.put("value",dicIllness.getIllcode());
                    map.put("label",dicIllness.getIllname());
                    list.add(map);
                }
            }
            return Response.ok(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }
}
